/*
 * Mantenimientos.java
 *
 * Created on 7 de mayo de 2008, 03:26 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package adsi.optimization;
import adsi.branchbound3.*;
import adsi.domain.EntityException;
import adsi.domain.EquipmentMaintenance;
import adsi.domain.Line;
import adsi.persistence.DataServiceException;
import adsi.persistence.DataServiceImpl;
import adsi.persistence.DataServiceModel;
import adsi.persistence.MaintenanceFactory;
import java.util.*;
/**
 *
 * @author Aramis
 */
public class Mantenimientos {
    
    /** Creates a new instance of Mantenimientos */
    Vector Mantenimientos = new Vector();
    Vector NoModificables = new Vector();
    Vector Modificables = new Vector();
    List<Line> idLineas;
    private MaintenanceFactory factory;
    private int numLineas;
    
    
    public Mantenimientos() throws DataServiceException, EntityException {
        DataServiceModel dsm = DataServiceImpl.getInstance();
        factory = MaintenanceFactory.getInstance();
        
        factory.retrieveMaintenances(null, "description");
        
        Map log = dsm.retrieveLog();
        List mantenimientos = new ArrayList();
        Set keys = log.keySet();
        List<EquipmentMaintenance> tmpMant;
        Tarea t;
        
        for(Object key: keys) {
            tmpMant = (List<EquipmentMaintenance>) log.get(key);
            for(EquipmentMaintenance em: tmpMant) {
                t = new Tarea(em.getID(), (GregorianCalendar) em.getMaintenanceDate(), factory.retrieveMaintenance(em.getMaintenanceID()).getDuration());
                System.out.println("Folio> " + em.getID());
                Mantenimientos.add(t);
            }
        }
        numLineas=dsm.retrieveLines().size();
        idLineas= dsm.retrieveLines();
        ordenaEDD();
        discrimina();
        
    }
    public List<Line> getIdLineas()
    {
        return idLineas;
    }
    
    public int getNumLineas()
    {
        return numLineas;
    }
    private void ordenaEDD()
    {
        int i,j=0,min;
        Tarea t;
        i=0;
        min=((Tarea) Mantenimientos.get(0)).getDj();
        for(i=0;i<Mantenimientos.size();i++)
        {
            for(j=0;j<Mantenimientos.size();j++)
            {
                if(((Tarea) Mantenimientos.get(i)).getDj() < ((Tarea) Mantenimientos.get(j)).getDj())
                {
                    t = ((Tarea) Mantenimientos.get(j));
                    Mantenimientos.set(j,((Tarea) Mantenimientos.get(i)));
                    Mantenimientos.set(i,t);
                }
            }
        }
        
    }
    
    public Vector regresaMantenimientos()
    {
        return Mantenimientos; //MANDAR PRIMERO LOS NO MODIFICABLES...e insertarlos en esa l�nea...
    }
    private void discrimina()
    {
        for(int i=0;i<Mantenimientos.size();i++)
            if(((Tarea) Mantenimientos.get(i)).modificable())
                NoModificables.add(i,(Tarea) Mantenimientos.get(i));
            else
                Modificables.add(i,(Tarea) Mantenimientos.get(i));
    }
    public Vector regresaMantenimientosNoModificables()
    {
        return NoModificables;
    }
    public Vector regresaMantenimientosModificables()
    {
       return Modificables; 
    }
    
}
